package com.rtwo.tio.server.handler;

import com.rtwo.tio.client.msg.JoinGroupRequest;
import com.rtwo.tio.common.BasicHandler;
import com.rtwo.tio.common.BasicPacket;
import com.rtwo.tio.common.MsgType;
import com.rtwo.tio.server.msg.JoinGroupResponse;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.utils.json.Json;

import java.io.UnsupportedEncodingException;

/**
 * Created by hasee on 2018/7/20.
 */
public class JoinGroupResponseHandler extends BasicHandler<JoinGroupRequest>{
    @Override
    public Class<JoinGroupRequest> getClazz() {
        return JoinGroupRequest.class;
    }

    @Override
    public void postHanlder(JoinGroupRequest body, ChannelContext channelContext) throws UnsupportedEncodingException {
        String groupName = body.getGroupName();
        Tio.bindGroup(channelContext,groupName);
        channelContext.setAttribute("groupName",groupName);
        String str = Json.toJson(new JoinGroupResponse(1));
        Tio.send(channelContext,new BasicPacket(MsgType.GROUP_JOIN_RESP,str.getBytes()));
    }
}
